<template>
  <div class="app-container">
    <div class="search-container">
      <el-form :model="searchParam" :inline="true">
        <!--        <el-form-item label="公司" style="width: 290px" prop="organizationId">-->
        <!--          <SelectOriganization-->
        <!--            :selectData="searchParam.organizationId"-->
        <!--            :isCheckStrictly="true"-->
        <!--            @change="getSelectIds"-->
        <!--            @change-data="organizationDataChange"-->
        <!--            :isMultiple="false"-->
        <!--            clearable-->
        <!--        /></el-form-item>-->
        <el-form-item label="工厂" style="width: 210px" prop="plantId">
          <el-select
            v-model="searchParam.plantId"
            @change="plantChange(searchParam.plantId)"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in plants"
              :key="item.id"
              :label="item.organizationCode"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="工作中心">
          <el-select
            v-model="searchParam.workCenterId"
            clearable
            filterable
            placeholder="请选择"
          >
            <el-option
              v-for="item in workShopList"
              :key="item.id"
              :label="item.workshopCode + '-' + item.workshopName"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="search">
            <i-ep-search />
            搜索
          </el-button>
          <el-button @click="reset">
            <i-ep-refresh />
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never" class="table-container">
      <!--      <template #header>-->
      <!--        <el-button type="success" plain @click="openDialog()"-->
      <!--        >-->
      <!--          <i-ep-plus />-->
      <!--          新增-->
      <!--        </el-button-->
      <!--        >-->
      <!--        <el-button-->
      <!--          type="danger"-->
      <!--          plain-->
      <!--          :disabled="ids.length === 0"-->
      <!--          @click="handleTableDelete()"-->
      <!--        >-->
      <!--          <i-ep-delete />-->
      <!--          删除-->
      <!--        </el-button-->
      <!--        >-->
      <!--      </template>-->

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <!--        <el-table-column type="selection" width="55" align="center" />-->
        <!--        <el-table-column-->
        <!--          label="公司编号"-->
        <!--          prop="organizationCode"-->
        <!--          min-width="100"-->
        <!--          show-overflow-tooltip-->
        <!--        />-->
        <el-table-column
          label="工厂"
          prop="plantCode"
          width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="停机时间"
          prop="stopTime"
          align="center"
          min-width="160"
          show-overflow-tooltip
        />
        <el-table-column
          label="工序"
          prop="processName"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          label="工作中心"
          prop="workCenterName"
          show-overflow-tooltip
          min-width="160"
        >
          <template #default="scope">
            {{ scope.row.workCenterCode + "-" + scope.row.workCenterName }}
          </template>
        </el-table-column>
        <el-table-column
          label="是否停产"
          prop="isStop"
          align="center"
          width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-tag type="danger" v-if="scope.row.isStop == 1">是</el-tag>
            <el-tag type="info" v-else>否</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="处理状态"
          prop="handleStatus"
          align="center"
          width="100"
          show-overflow-tooltip
        >
          <template #default="scope">
            <el-tag v-if="scope.row.handleStatus == 0">处理中</el-tag>
            <el-tag type="success" v-else>处理完成</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          label="复工时间"
          prop="handleTime"
          align="center"
          width="180"
          show-overflow-tooltip
        />

        <el-table-column
          label="停机时长(min)"
          prop="stopTimeCount"
          align="right"
          width="120"
          show-overflow-tooltip
        />

        <el-table-column
          label="创建时间"
          prop="createTime"
          align="center"
          width="180"
          show-overflow-tooltip
        />
        <el-table-column
          label="创建人"
          prop="createdUserName"
          align="left"
          show-overflow-tooltip
        />
        <!-- <el-table-column fixed="right" label="操作" width="240">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialogdetail(scope.row)"
              ><i-ep-search />
              查看
            </el-button>
            <el-button
              type="primary"
              size="small"
              link
              @click="openDialog(scope.row)"
              ><i-ep-edit />编辑</el-button
            >
            <el-button
              type="primary"
              size="small"
              link
              @click="handleTableDelete(scope.row.id)"
              ><i-ep-delete />删除</el-button
            >
          </template>
        </el-table-column> -->
      </el-table>

      <el-pagination
        :background="true"
        :current-page="pageable.pageNum"
        :page-size="pageable.pageSize"
        :page-sizes="[10, 25, 50, 100]"
        :total="pageable.total"
        layout="total, sizes, prev, pager, next, jumper"
        class="mt-[20px]"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <andrework-edit
      :visible="addDialog.visible"
      :title="addDialog.title"
      :data="addDialog.data"
      @reset-table="(id:string) => (id?.length ? getTableList() : reset())"
      :is-readonly="addDialog.readvisible"
      @change-visible="changeVisible"
    />
  </div>
</template>

<script setup lang="ts">
import { useTable, useDownload, useDictionary } from "@/utils/useTable";
import request from "@/utils/request";
import { getWorkShopList } from "@/utils/commonData";

defineOptions({
  name: "ANDONANDREWORK",
  inheritAttrs: false,
});

// 请求参数
const requestApi = "/andon/andrework/queryByPage";

// 是否需要分页
const pagination = true;

const initParam = {};

// 初始化搜索参数数据
const searchParamInit = {
  organizationId: "",
  plantId: "",
  workCenterId: "",
};

const deleteParam = {
  api: "/andon/andrework/delete",
  data: {},
};

// 数据请求错误后处理
function requestError() {}

const dataCallBack = ref(undefined); // 无需二次数据处理&不传赞数也行
let plants = ref([]);
let workShopList = ref([]);
// 表格操作 Hooks
const {
  tableData,
  pageable,
  searchParam,
  ids,
  getTableList,
  search,
  reset,
  handleSizeChange,
  handleCurrentChange,
  handleSelectionChange,
  handleTableDelete,
  loading,
}: any = useTable(
  requestApi,
  initParam,
  searchParamInit,
  pagination,
  dataCallBack.value,
  requestError,
  deleteParam
);

onMounted(() => {
  getTableList();
  getPlants();
});

function getSelectIds(ids) {
  searchParam.value.organizationId = ids;
  getPlants();
}

function organizationDataChange(datas: [] | {}) {
  searchParam.organizationName = datas.organizationName;
  searchParam.organizationCode = datas.organizationCode;
}

async function getPlants() {
  const data = await request({
    url: "/system/sysOrganization/getList",
    method: "post",
    data: { organizationType: 3 },
  });
  plants.value = data.data;
  searchParam.value.plantId = plants.value[0].id;
  plantChange(searchParam.value.plantId);
}

function plantChange(plantId) {
  getWorkShop(plantId);
}

async function getWorkShop(plantId) {
  const data = await request({
    url: "/mdm/mdmworkshop/getList",
    method: "post",
    data: { plantId: plantId },
  });
  workShopList.value = data.data;
}

const addDialog = reactive<DialogOption>({
  visible: false,
  readvisible: false,
  title: "",
  data: {},
});

// 查询详情
async function getInfo(id: string) {
  request({
    url: `/andon/andrework/get/${id}`,
    method: "get",
  })
    .then((data) => {
      addDialog.data = data.data;
      addDialog.visible = true;
    })
    .catch((res) => {
      console.log(res);
    });
}

/** 打开复工表单弹窗 */
function openDialog(roleObj?: Object) {
  if (roleObj) {
    addDialog.title = "修改复工";
    getInfo(roleObj.id);
  } else {
    addDialog.title = "新增复工";
    addDialog.visible = true;
  }
  addDialog.readvisible = false;
}

/** 打开复工表单弹窗 详情 */
function openDialogdetail(roleObj?: any) {
  addDialog.title = "复工详情";
  getInfo(roleObj.id);
  addDialog.readvisible = true;
}

function changeVisible(value: boolean) {
  addDialog.visible = value;
  if (!value) {
    addDialog.data = {};
  }
}
</script>
<style lang="scss" scoped>
:deep(.el-form .el-form-item__label) {
  width: 63px;
}
</style>
